package com.gec.controller;

import com.gec.dao.BookDao;
import com.gec.dao.BookDao1;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

//下面这个url地址 它的/是不可以漏 漏写 项目启动不了
@WebServlet("/AddBookServlet")
public class AddBookServlet extends HttpServlet {


    //接受页面提交过来的客房数据 然后把它添加到数据库中
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取页面的form表单提交过来的数据

        String name = request.getParameter("name");
        String author = request.getParameter("author");
        String price = request.getParameter("price");
        String id = request.getParameter("id");
        System.out.println(name+author+price);
        //编写sql语句将 form表单的数据添加到数据库表
        String  sql = "insert into book values (?,?,?,?)";
        //用一个集合来暂存要添加的数据
        String  sql1 = "insert into book1 values (?,?,?,?)";
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(id);
        objects.add(name);
        objects.add(author);
        objects.add(price);

        //创建bookDao来操作
        BookDao bookDao = new BookDao();
        //执行sql语句 传入数据
        bookDao.update(sql,objects);
        BookDao1 bookDao1 = new BookDao1();
        bookDao1.update(sql1,objects);
        //添加成功以后将页面重定向 图书列表页面 这样子新增的客房信息就会显示出来了
        response.sendRedirect("BookListServlet");
    }
}
